﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace DoAn
{
    public partial class frmPhat : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        SqlCommandBuilder cmb;
        public frmPhat()
        {
            String conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            conn = new SqlConnection(conStr);
            conn.ConnectionString = conStr;
            InitializeComponent();
        }

        private void frmPhat_Load(object sender, EventArgs e)
        {
            //conn.Open();
            cmd = new SqlCommand("SELECT * FROM tinhtienphat", conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "tinhtienphat");
            cmb = new SqlCommandBuilder(da);
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            tbmttp.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            tbmhd.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            tbmakh.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            tbtkh.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            tbsotv.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            cbhtv.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            cbmuclai.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();
            cbtll.Text = dataGridView1.CurrentRow.Cells[7].Value.ToString();
            tbtienlai.Text = dataGridView1.CurrentRow.Cells[8].Value.ToString();
            dtngaydl.Text = dataGridView1.CurrentRow.Cells[9].Value.ToString();
            tbtienphat.Text = dataGridView1.CurrentRow.Cells[10].Value.ToString();
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbmttp.Text == "" || tbmhd.Text == "" || tbmakh.Text == "" || tbtkh.Text == "" || tbsotv.Text == "" || cbhtv.Text == "" || cbmuclai.Text == "" || cbtll.Text == "" || tbtienlai.Text == "" || dtngaydl.Text == "" || tbtienphat.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền đầy đủ thông tin", "Có lỗi xảy ra!");
                    tbmttp.Focus();
                    return;
                }
                else
                {
                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM tinhtienphat", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "tinhtienphat");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                    DataTable dt = ds.Tables[0];
                    DataRow newRow = dt.NewRow();
                    newRow["matinhtienphat"] = tbmttp.Text;
                    newRow["mahd"] = tbmhd.Text;
                    newRow["makh"] = tbmakh.Text;
                    newRow["tenkhachhang"] = tbtkh.Text;
                    newRow["sotienvay"] = tbsotv.Text;
                    newRow["htvten"] = cbhtv.Text;
                    newRow["tllmuclai"] = cbmuclai.Text;
                    newRow["mptyle"] = cbtll.Text;
                    newRow["tienlai"] = tbtienlai.Text;
                   newRow["ngayphaidonglai"] = dtngaydl.Text;
                    newRow["tienphat"] = tbtienphat.Text;
                    dt.Rows.Add(newRow);
                    da.Update(ds, "tinhtienphat");
                    MessageBox.Show("Thêm thành công");
                }

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
                tbmttp.Focus();

            } 
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.CurrentRow.Index;
            ds.Tables[0].Rows[i].Delete();
            da.Update(ds, "tinhtienphat");
            MessageBox.Show("Xóa thành công");
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            frmMenu objfrmMenu = new frmMenu();
            this.Dispose();
            objfrmMenu.ShowDialog();
        }

        private void bttimkiemmakh_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbtimkiemmakh.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền tên", "Có lỗi xảy ra!");
                    tbtimkiemmakh.Focus();
                    return;
                }
                else
                {

                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM tinhtienphat where makh  = '" + tbtimkiemmakh.Text + "'", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "tinhtienphat");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bttimkiemmahd_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbtimkiemmahd.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền tên", "Có lỗi xảy ra!");
                    tbtimkiemmahd.Focus();
                    return;
                }
                else
                {

                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM tinhtienphat where mahd  = '" + tbtimkiemmahd.Text + "'", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "tinhtienphat");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbmttp.Text == "")
                {
                    MessageBox.Show("Bắt đầu sửa", "Thông báo!");
                    tbmttp.Focus();
                    return;
                }
                else
                {
                    String matinhtienphat = tbmttp.Text;
                    String mahd = tbmhd.Text;
                    String makh = tbmakh.Text;
                    String tenkhachhang = tbtkh.Text;
                    String sotienvay = tbsotv.Text;
                    String htvten = cbhtv.Text;
                    String tllmuclai = cbmuclai.Text;
                    String mptyle = cbtll.Text;
                    String tienlai = tbtienlai.Text;
                    String ngayphaidonglai = dtngaydl.Value.ToString();
                    String tienphat = tbtienphat.Text;

                    int matinhtienphat1= Int32.Parse(matinhtienphat);
                    int mahd1 = Int32.Parse(mahd);
                    int makh1 = Int32.Parse(makh);
                    int sotienvay1 = Int32.Parse(sotienvay);
                    int tienlai1 = Int32.Parse(tienlai);
                    int tienphat1 = Int32.Parse(tienphat);
                    conn.Open();
                    //String sql = 
                    cmd = new SqlCommand("Update tinhtienphat set mahd=" + mahd1 + ", makh = " + makh1 + ", tenkhachhang= ' " + tenkhachhang + " ',sotienvay = " + sotienvay1 + ",htvten = '" + htvten + "',tllmuclai = '" + tllmuclai + "', mptyle = '" + mptyle + "',tienlai = " + tienlai1 + " ,ngayphaidonglai =' " + ngayphaidonglai + " ',tienphat = " + tienphat1 + "where matinhtienphat=" + matinhtienphat1 + "", conn);
                    da = new SqlDataAdapter(cmd);
                    int i = cmd.ExecuteNonQuery();
                    ds = new DataSet();
                    da.Fill(ds, "tinhtienphat");
                    cmb = new SqlCommandBuilder(da);
                    //dataGridView1.DataSource = ds.Tables[0];
                    // this.frmHopdongvayvon_Load();
                    //this.LoadForm();
                    this.frmPhat_Load(sender, e);
                    MessageBox.Show("Sửa thành công");
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show(" thành công");
            }
        }
    }
}
